#include "TurnNeckToBehaviour.h"


/*
 * Otocenie krku na dane miesto. Tato funkcia sluzi na zaznamenanie udajov potrebnych
 * na otocenie. 
 * 
 * @param	futurePosition	oznacuje nasledujucu poziciu krku
 * @param	myFuturePosition	oznacuje hracovu buducu poziciu
 * @param	nextTurn	uhol? dalsieho otocenia? ten kod je skutocne bombovo komentovany
 *
 */
void TurnNeckToBehaviour::Behave(const Point& futurePosition, const Point& myFuturePosition, float nextTurn)
{
	DATA_STORAGE.turnNeck.futurePosition = futurePosition;
	DATA_STORAGE.turnNeck.futureMyPosition = myFuturePosition;
	DATA_STORAGE.turnNeck.nextTurn = nextTurn;

	// Ak je synchronizacia krku s vizualnou informaciou vypnuta
	if(world->me->bNeckSynchronization == false)
	{
		DATA_STORAGE.turnNeck.avoidNextCorrection = true;
		NeckSynchronizationWithVI(NEW_VI);
	}
}

/*
 * Synchronizuje krkove metody s prichadzajucou vizualnou informaciou
 *
 * @param mode	co sa ma urobit s krkom (pohladom), vid TurnNeckToBehaviour.h pre popis parametrov
 */
void TurnNeckToBehaviour::NeckSynchronizationWithVI(int mode)
{
	switch(mode)
	{
		case ALIG_NECK:
		{
			GetAlignNeckBehaviour()->Behave();
			world->me->bSendTurnNeckTo = false;
			DATA_STORAGE.turnNeck.avoidNextCorrection = true;
			DATA_STORAGE.turnNeck.changeView = 0;
			return;
		}

		case NAR_VIEW:
		{
			if(world->me->bNeckSynchronization == true)
			{
				DATA_STORAGE.turnNeck.changeView = 2;
				DATA_STORAGE.turnNeck.avoidNextCorrection = true;
			}
			else
				commandGenerator->ChangeView(NARROW_VIEW);

			return;	
		}

		// Prisiel prikaz na vykonanie jednej z krkovych metod (na zaklade najnovsej VI)
		case NEW_VI:
		{
			// Funkcia AlignNeck ma vacsiu prioritu ako TurnNeckTo	

			switch(world->me->GetViewWidth())
			{
				case NARROW_VIEW:
					DATA_STORAGE.turnNeck.correction = 45;
					break;

				case NORMAL_VIEW:
					DATA_STORAGE.turnNeck.correction = 90;
					break;

				case WIDE_VIEW:
					DATA_STORAGE.turnNeck.correction = 180;
					break;
			}

			if(DATA_STORAGE.turnNeck.avoidNextCorrection == false)
			{
				// Korekcia - k uhlu pridava, +/-1 * (SirkaPohladu / 3)
				DATA_STORAGE.turnNeck.correction = float(DATA_STORAGE.turnNeck.neckDirection * 
					(DATA_STORAGE.turnNeck.correction / 3.0));
				// Zmenime polaritu natocenia krku
				DATA_STORAGE.turnNeck.neckDirection  *= -1;
			}
			else
			{
				DATA_STORAGE.turnNeck.correction = 0.0;
			}

			GetTurnNeckToAuxBehaviour()->Behave(DATA_STORAGE.turnNeck.correction);

			if(DATA_STORAGE.turnNeck.changeView == 1)
			{
				commandGenerator->ChangeView(NORMAL_VIEW);
			}

			else if(DATA_STORAGE.turnNeck.changeView == 2)
			{
				commandGenerator->ChangeView(NARROW_VIEW);
			}
			
			world->me->bSendTurnNeckTo = false;
			DATA_STORAGE.turnNeck.avoidNextCorrection = false;
			DATA_STORAGE.turnNeck.changeView = 0;

			return;
		}

		case NOR_VIEW:
		{
			if(world->me->bNeckSynchronization == true)
			{
				DATA_STORAGE.turnNeck.changeView = 1;
				DATA_STORAGE.turnNeck.avoidNextCorrection = true;
			}
			else
				commandGenerator->ChangeView(NORMAL_VIEW);

			return;
		}
	
		default:
			break;
	}
}
